package com.itheima.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.*;
import com.itheima.dto.PmsProductDto;
import com.itheima.service.db.*;
import com.itheima.util.R;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class PmsProductManager {

    @DubboReference
    PmsProductService pmsProductService;

    @DubboReference
    PmsMemberPriceService pmsMemberPriceService;

    @DubboReference
    CmsPrefrenceAreaProductRelationService cmsPrefrenceAreaProductRelationService;

    @DubboReference
    PmsProductAttributeValueService pmsProductAttributeValueService;

    @DubboReference
    PmsProductFullReductionService pmsProductFullReductionService;

    @DubboReference
    PmsProductLadderService pmsProductLadderService;

    @DubboReference
    PmsSkuStockService pmsSkuStockService;

    @DubboReference
    CmsSubjectProductRelationService cmsSubjectProductRelationService;
    /**
     * 查询商品
     *
     * @param brandId           商品品牌编号
     * @param keyword           商品名称模糊关键字
     * @param pageNum
     * @param pageSize
     * @param productCategoryId 商品分类编号
     * @param productSn         商品货号
     * @param publishStatus     上架状态
     * @param verifyStatus      审核状态
     * @return
     */
    public R getByIdProduct(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {

        Page<PmsProduct> res = pmsProductService.getByIdProduct(brandId, keyword, pageNum, pageSize, productCategoryId, productSn, publishStatus, verifyStatus);

        return R.success(new PageBeanVo(pageNum,pageSize,res.getTotal(),res.getRecords()));

    }

    public R deleteStatus(Integer deleteStatus, Integer[] ids) {
        Boolean flag = pmsProductService.deleteStatus(deleteStatus, ids);
        return flag?R.success(""):R.error(500,"操作失败");
    }

    public R newStatus(Integer newStatus, Integer[] ids) {
        Boolean flag = pmsProductService.newStatus(newStatus, ids);
        return flag?R.success(""):R.error(500,"操作失败");
    }

    public R publishStatus(Integer publishStatus, Integer[] ids) {
        Boolean flag = pmsProductService.publishStatus(publishStatus, ids);
        return flag?R.success(""):R.error(500,"操作失败");
    }

    public R recommendStatus(Integer recommendStatus, Integer[] ids) {
        Boolean flag = pmsProductService.recommendStatus(recommendStatus, ids);
        return flag?R.success(""):R.error(500,"操作失败");
    }

    public R updateInfoId(Integer id) {
        PmsProductDto dto = new PmsProductDto();
        PmsProduct pmsProduct =pmsProductService.selectById(id);
        BeanUtils.copyProperties(pmsProduct,dto);

        //商品会员价格表
        List<PmsMemberPrice> pmsMemberPriceList=pmsMemberPriceService.selectByProductId(id);
        dto.setMemberPriceList(pmsMemberPriceList);
        //优选专区和商品的关系
        List<CmsPrefrenceAreaProductRelation> cmsPrefrenceAreaProductRelationList=cmsPrefrenceAreaProductRelationService.selectByProductId(id);
        dto.setPrefrenceAreaProductRelationList(cmsPrefrenceAreaProductRelationList);
        //存储产品参数信息表
        List<PmsProductAttributeValue> pmsProductAttributeValueList=pmsProductAttributeValueService.selectByProductId(id);
        dto.setProductAttributeValueList(pmsProductAttributeValueList);
        //产品满减表
        List<PmsProductFullReduction> pmsProductFullReductions=pmsProductFullReductionService.selectByProductId(id);
        dto.setProductFullReductionList(pmsProductFullReductions);
        //产品阶梯价格表
        List<PmsProductLadder> pmsProductLadderList=pmsProductLadderService.selectByProductId(id);
        dto.setProductLadderList(pmsProductLadderList);
        //sku的库存
        List<PmsSkuStock> pmsSkuStockList=pmsSkuStockService.selectByProductId(id);
        dto.setSkuStockList(pmsSkuStockList);
        //专题商品的关系表
        List<CmsSubjectProductRelation> cmsSubjectProductRelationList=cmsSubjectProductRelationService.selectByProductId(id);
        dto.setSubjectProductRelationList(cmsSubjectProductRelationList);

        return R.success(dto);

    }

    public R updateproductById(Integer id, PmsProductDto dto) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(dto,pmsProduct);
        pmsProductService.updateById(pmsProduct,id);

        //商品会员价格表
        pmsMemberPriceService.updateByProductId(id,dto.getMemberPriceList());
        //优选专区和商品的关系
        cmsPrefrenceAreaProductRelationService.updateByProductId(id,dto.getPrefrenceAreaProductRelationList());
        //存储产品参数信息表
        pmsProductAttributeValueService.updateByProductId(id,dto.getProductAttributeValueList());
        //产品满减表
        pmsProductFullReductionService.updateByProductId(id,dto.getProductFullReductionList());
        //产品阶梯价格表
        pmsProductLadderService.updateByProductId(id,dto.getProductLadderList());
        //sku的库存
        pmsSkuStockService.updateByProductId(id,dto.getSkuStockList());
        //专题商品的关系表
        cmsSubjectProductRelationService.updateByProductId(id,dto.getSubjectProductRelationList());

        return R.success("");
    }

    public R save(PmsProductDto dto) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(dto,pmsProduct);
        Long productId=pmsProductService.save(pmsProduct);
        //商品会员价格表
        pmsMemberPriceService.save(productId,dto.getMemberPriceList());
        //优选专区和商品的关系
        cmsPrefrenceAreaProductRelationService.save(productId,dto.getPrefrenceAreaProductRelationList());
        //存储产品参数信息表
        pmsProductAttributeValueService.save(productId,dto.getProductAttributeValueList());
        //产品满减表
        pmsProductFullReductionService.save(productId,dto.getProductFullReductionList());
        //产品阶梯价格表
        pmsProductLadderService.save(productId,dto.getProductLadderList());
        //sku的库存
        pmsSkuStockService.save(productId,dto.getSkuStockList());
        //专题商品的关系表
        cmsSubjectProductRelationService.save(productId,dto.getSubjectProductRelationList());

        return R.success("");
    }
}
